package com.dongxing.concyrrency.chapter5;

import java.util.Optional;
import java.util.stream.IntStream;

/**
 * @author XavierDong
 **/
public class ThreadJoin {

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            IntStream.range(1, 1000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i));

        });
        Thread t2 = new Thread(() -> {
            IntStream.range(1, 1000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i));

        });
        Thread t3 = new Thread(() -> {
            IntStream.range(1, 1000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i));

        });
        t1.start();
        t2.start();
        try {
            // 一直等到这个t1线程运行完毕后，才会执行main线程。但是两个子线程之间是有交互的。
            // 只是main的线程会等到，自己的子线程结束，才能运行
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
        Optional.of("All of tasks finish done.").ifPresent(System.out::println);
        IntStream.range(1, 1000).forEach(i -> System.out.println(Thread.currentThread().getName() + i));
    }
}
